<template>
  <div>
    <!-- card:展示三级联动 -->
    <el-card>
      <Category @getCategoryId="getCategoryId" />
    </el-card>
    <el-card style="margin: 20px 0">
      <!-- 场景0 -->
      <div v-show="scene==0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id"
        @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 展示SPU(类的地方) -->
        <el-table
          style="width: 100%; margin: 10px 0"
          border
          :data="records"
          v-loading="loading"
          element-loading-text="玩命加载中..."
          element-loading-spinner="el-icon-loading"
        >
          <el-table-column label="序号" width="80" type="index" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="success"
                size="mini"
                title="添加SKU"
                icon="el-icon-plus"
              ></el-button>
              <el-button
                type="warning"
                size="mini"
                title="修改SKU"
                icon="el-icon-edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="mini"
                title="查看SUP的全部的SKU"
                icon="el-icon-info"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                title="删除SUP"
                icon="el-icon-delete"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 
            分页器
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
          :pager-count="9"  设置连续页码数
         -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          prev-text="上一页"
          next-text="下一页"
          layout="prev, pager, next, jumper,->, sizes,total "
          :total="total"
          :pager-count="9"
          background
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene==1" @changeScene="changeScene"  ref="spu"></SpuForm>
      <SkuForm v-show="scene==2"></SkuForm>
    </el-card>
  </div>
</template>

<script>
// 引入子组件
import SpuForm from './SpuForm';
import SkuForm from './SkuForm';
export default {
    components:{
        SpuForm,
        SkuForm
    },
  name: "Spu",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1, //分页器的当前页码
      limit: 3, //当前一页展示几条数据
      total: 0, //一共的数据
      records: [], //展示数据
      loading: false, //loading 加载属性
      scene: 0, //  切换场景属性
    };
  },
  methods: {
    getCategoryId({ id, flag }) {
      switch (flag) {
        case 1:
          this.category1Id = id;
          this.category2Id = "";
          this.category3Id = "";
          break;
        case 2:
          this.category2Id = id;
          this.category3Id = "";
          break;
        case 3:
          this.category3Id = id;
          //   发请求-获取Spu列表的数据进行展示
          // 发请求之前loading开始转圈圈
          this.getSpuList();
          this.loading = true;
          break;
      }
    },
    // 获取SPU(类的)实例进行展示
    async getSpuList(pager = 1) {
      this.loading = true;
      this.page = pager;
      // 先整理参数 page limit category3Id
      let { page, limit, category3Id } = this;
      let result = await this.$ajax.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        // 存储数据
        this.total = result.data.total;
        this.records = result.data.records;
        this.loading = false;
      }
    },
    // 当pagesize发生变化的时候触发
    handleSizeChange(limit) {
        // 整理参数
      this.limit = limit;
      this.getSpuList();
    },
    // 添加Spu的回调
    addSpu(){
        this.scene=1;
    },
    // 修改spu的回调
    updateSpu({id}){
        this.scene=1;
        // 通过ref实现父组件获取子组件(子组件的数据+方法)
        this.$refs.spu.initData(id);
    },
    changeScene(scene) {
        this.scene=scene;
    }
  },
};
</script>

<style>
</style>